/*** 
 * @_______________#########_______________________ 
 * @______________############_____________________ 
 * @______________#############____________________ 
 * @_____________##__###########___________________ 
 * @____________###__######_#####__________________ 
 * @____________###_#######___####_________________ 
 * @___________###__##########_####________________ 
 * @__________####__###########_####_______________ 
 * @________#####___###########__#####_____________ 
 * @_______######___###_########___#####___________ 
 * @_______#####___###___########___######_________ 
 * @______######___###__###########___######_______ 
 * @_____######___####_##############__######______ 
 * @____#######__#####################_#######_____ 
 * @____#######__##############################____ 
 * @___#######__######_#################_#######___ 
 * @___#######__######_######_#########___######___ 
 * @___#######____##__######___######_____######___ 
 * @___#######________######____#####_____#####____ 
 * @____######________#####_____#####_____####_____ 
 * @_____#####________####______#####_____###______ 
 * @______#####______;###________###______#________ 
 * @________##_______####________####______________ 
 * @
 * @Author: ipk518 18163976442@163.com
 * @Date: 2024-11-23 10:58:12
 * @LastEditors: ipk518 121206530@qq.com
 * @LastEditTime: 2025-08-19 15:39:52
 * @FilePath: /etws-sxk2307/inc/autoCalibration.h
 * @Description: 
 * @************佛祖保佑************
 * @Copyright (c) 2024 by etws@quyujiang, All Rights Reserved. 
 */

#ifndef _AUTOCALIBRATION_H_
#define _AUTOCALIBRATION_H_

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <iostream>

#include <mutex>
#include <condition_variable>


using namespace std;

class queueShare;
class shareSub;


//校准的任务次数
typedef enum num_task
{
    t_changeFreq0 = 0,
    t_changeFreq1 = 1,
    t_changeEnd,
}freqTask;

//校准任务流程
typedef enum  uio_task
{
    t_init,
    t_wait,
    t_getGps,
    t_dueNode,
    t_timingHandle, //触发自动校准
    t_waitSrcVal,
    t_hSendValHandle,
    t_vSendValHandle,
    t_hRecvValHandle,
    t_vRecvValHandle,
    t_cyclicTask,
    /*雷象触发模式配置*/
    t_hSendCali,    //不减耦合系数、不减通道系数 H通道
    t_vSendCali,    //不减耦合系数、不减通道系数 V通道
    t_hRecvCali,    //接收校准
    t_vRecvCali,    //接收校准
    t_hCyclicTask,      //更新H通道系数,发射
    t_vCyclicTask,      //更新V通道系数，发射
    t_hSubtractCoffeTask,    //减通道系数，发射
    t_vSubtractCoffeTask,    //减通道系数，发射
    t_hSubCoffeTask,    //减耦合系数 H通道，发射
    t_vSubCoffeTask,    //减耦合系数 V通道，发射
    t_hCSubCoffeTask,   //减耦合系数、减通道系数 H通道，发射
    t_vCSubCoffeTask,   //减耦合系数、减通道系数 V通道，发射
    t_hRecvCycleTask,  //H接收，不加任何系数
    t_vRecvCycleTask,   //V接收，不加任何系数
    t_hRecvSubCoffTask, //H接收，减耦合系数
    t_vRecvSubCoffTask, //V接收，减耦合系数

    /*雷象触发模式配置*/
    t_end,
}taskprocess;


//下发DDS的数据结构体
struct __attribute__ ((__packed__)) ap_Control_t{
        uint16_t freq;
        uint16_t ddsAmp[32];
        uint16_t ddsPhase[32];
};

//本地保存的基准幅相结构体
struct __attribute__ ((__packed__)) tr_mulit_freq_t{
        uint16_t hRecAmp[32];
        uint16_t vRecAmp[32];
        uint16_t hSendAmp[2][32];
        uint16_t hSendPhase[2][32];
        uint16_t vSendAmp[2][32];
        uint16_t vSendPhase[2][32];
};
//接收的基准值结构体,用于接收校准
struct __attribute__ ((__packed__)) recv_mulit_freq_t{
    uint16_t hRecvAmp[2][32];
    uint16_t hRecvPhase[2][32];
    uint16_t vRecvAmp[2][32];
    uint16_t vRecvPhase[2][32];
};

struct __attribute__ ((__packed__)) ap_update_t{
        uint16_t freq;
        short ddsAmp[32];
        short ddsPhase[32];
};

class autoCalib : public shareSub
{
public:
    void update(int m_flag,vector<uint8_t>& dataIn) override;
    void test(char* m,int len) override;
};

class crontab_uio : public shareSub
{
public:
    // friend class Uio;
    
    crontab_uio();
    ~crontab_uio();
public:
    void update(int m_flag,vector<uint8_t>& dataIn) override;
    void test(char* m,int len) override;
public:

    void init();
    void task();
    bool wait(uioMode m);
    // void recv(uioMode st,unsigned char *dataIn,unsigned int dataLen);
    int send(uioMode to,unsigned char *dataOut,unsigned int dataLen);
    void send(uioMode to);
    bool getGps(unsigned long dueTime);
    void autoPoll();
    bool startOperate();
    void setOperate(bool ret);
    void setFreq(int frequeue);
    void startCali(uioMode st);
    int readCouplingParameters();
    bool m_RunFlag;
    
private:
    
    uint8_t m_tbfEn = 0;
    std::thread *CrontabFd;/*定时线程线程*/
    static void crontab_Thread_Func(crontab_uio *argc);//定时任务
    void clearAPStatus(uioMode st,int freq);
    uioMode calcOffsetValue(uioMode st,unsigned int freq, bool update);
    uioMode calcOffsetValue2(uioMode st,unsigned int freq, bool update,int apMode);
    void subtractCouplingCoefficient(uioMode st, unsigned int freq, uint16_t* calibrationData) ;
    bool getCurrentCalibration(uioMode st,unsigned char *dataOut,int len);
    void updateAmplitudeOffset(uioMode st, unsigned int freq,const uint16_t* srcData,const uint16_t *standAmpData) ;
    void updatePhaseOffset(uioMode st, unsigned int freq,const uint16_t* srcData,const uint16_t *standAmpData) ;
    void updateRecvOffset(uioMode st, unsigned int freq,const uint16_t* srcData,const uint16_t *standData) ;
    void recvCompensation(uioMode st, unsigned int freq, int mode, struct ap_Control_t& apCtrl);
    uint16_t g_usSendData[64];
    // struct ap_update_t webCache_H;//用于上报雷象上位机
    // struct ap_update_t webCache_V;//用于上报雷象上位机
    struct ap_update_t webCache;//用于上报雷象上位机接收

    //任务流程
    taskprocess taskProcess;
    taskprocess taskProcess1;
    int taskFreq;
    int g_ifreq = 0;//备份频点
    int g_manualAmpCnt = 0;//手动补平计数
    int g_manualPhaCnt = 0;

    //用于自动和手动校准
    class queueShare *m_hSendCaliQueue;
    class queueShare *m_vSendCaliQueue;
    class queueShare *m_hRevCaliQueue;
    class queueShare *m_vRevCaliQueue;
    
    std::mutex g_CaliMutex;
    std::condition_variable cv;


    void subCouplingCoffe(uioMode st);
    void sendCalibSubC(uioMode st);
    void sendCalibration(uioMode st,bool erase);
};

extern struct tr_mulit_freq_t mulitFreqStr;

extern struct recv_mulit_freq_t recvMulitFreqStr;

extern struct ap_Control_t ddsCacheStr_H;
extern struct ap_Control_t ddsCacheStr_V;
extern struct ap_Control_t ddsSendStr;


#endif

